package net.cmkj.mine.bean;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import net.cmkj.mine.bean.base.BaseCalculate;
import net.cmkj.mine.util.BeanUtils;
import net.cmkj.mine.util.CommonUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class Calculate extends BaseCalculate<Calculate> {

    public static final Calculate dao = new Calculate().dao();

    private static Logger log = LogManager.getLogger(Calculate.class);

    /**
     * 获取计算公式列表
     * @param pageNumber
     * @param pageSize
     * @param moduleid
     * @return
     * @throws Exception
     */
    public Page<Calculate> getList(int pageNumber, int pageSize, long moduleid) throws Exception {
        return this.paginate(pageNumber, pageSize, "select t.*, c.column_comment ", "from calculate t left join columns c on t.columnid = c.id where t.moduleid = ? order by t.id desc", moduleid);
    }

    /**
     * 获取计算公式列表
     * @param moduleid
     * @return
     * @throws Exception
     */
    public List<Calculate> getList(long moduleid) throws Exception {
        return this.find("select t.*, c.column_name, c.column_comment from calculate t left join columns c on t.columnid = c.id where t.moduleid = ? order by t.id desc", moduleid);
    }

    /**
     * 计算公式保存
     * @param cal
     * @return
     */
    public boolean save(Calculate cal) throws Exception {
        log.info("对象保存");
        boolean flag = false;
        if (cal.getId() == null) {
            flag = cal.save();
        } else {
            Calculate src = cal.findById(cal.getId());
            log.info(BeanUtils.compare(src, cal));
            flag = cal.update();
        }
        return flag;
    }

    /**
     * 根据模块id删除计算公式
     * @param moduleid
     * @return
     * @throws Exception
     */
    public boolean deleteByModuleid(Long moduleid) throws Exception {
        Db.update("delete from calculate where moduleid = ?", moduleid);
        return true;
    }

    /**
     * 获取可参与计算公式字段列表
     * @param moduleid
     * @return
     * @throws Exception
     */
    public List<Record> getCalColumnList(Long moduleid) throws Exception {
        return Db.find("select t.id, t.column_comment as name from columns t where t.moduleid = ? and t.column_type in ('double','int') and t.id not in (select columnid from calculate where moduleid = t.moduleid)", moduleid);
    }

    /**
     * 获取被参与计算公式字段列表
     * @param moduleid
     * @param columnid
     * @return
     * @throws Exception
     */
    public List<Columns> getCaledColumnList(Long moduleid, Long columnid) throws Exception {
        List<Object> params = new ArrayList<Object>();
        String sql = "select t.* from columns t where t.moduleid = ? ";
        params.add(moduleid);
        if (columnid != null) {
            sql += " and t.id != ?";
            params.add(columnid);
        }
        return Columns.dao.find(sql, CommonUtils.listToArr(params));
    }

}
